package com.ticket.sass.admin.business.impl;


import com.ticket.sass.admin.business.FileUpLoadBusiness;
import com.ticket.sass.admin.config.AppConfig;
import com.ticket.sass.admin.model.QiNiuModel;

import com.ticket.sass.admin.util.RedisCacheUtil;
import com.ticket.sass.common.file.factory.FileServiceFactory;

import com.ticket.sass.common.file.service.FileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * @author ywb
 * @date 2022-09-28 10:13
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class FileUpLoadBusinessImpl implements FileUpLoadBusiness {
    
    final RedisCacheUtil redisCacheUtil;
    
    @Override
    public QiNiuModel qiNiuModel() {
        FileService service = FileServiceFactory.getFileService();
        return new QiNiuModel(
            service.uptoken(),
            "",
            AppConfig.KEY_PREFIX
        );
    }
    
    @Override
    public String exportToken(String route) {
        String hashString = Integer.toHexString(UUID.randomUUID().toString().hashCode());
        redisCacheUtil.setCacheObject(route + ":" + hashString, 1);
        redisCacheUtil.setKeyExpireTime(route + ":" + hashString, 5, TimeUnit.MINUTES);
        return hashString;
    }
    
    @Override
    public Boolean existExportToken(String token, String route) {
        Boolean exist = redisCacheUtil.getCacheObject(route + ":" + token) != null;
        redisCacheUtil.deleteObject(route + ":" + token);
        return exist;
    }
}
